package com.ms.common.utils;

import cn.hutool.core.util.StrUtil;

public class StringUtils extends org.apache.commons.lang3.StringUtils {

    /**
     * @param str
     * @return: String
     * @description:去掉空白字符
     */
    public static String trim(Object str) {
        return str == null ? null : valueOf(str).trim();
    }

    /**
     * @param str
     * @return: String
     * @description:对象转字符串
     */
    public static String valueOf(Object str) {
        if (str == null) {
            return "";
        } else {
            return String.valueOf(str);
        }

    }

    /**
     * 拼接字符串
     *
     * @param objects source
     * @return result
     */
    public static String concat(Object... objects) {
        if (objects == null || objects.length == 0) {
            return null;
        }
        StringBuilder result = new StringBuilder(8);
        for (Object object : objects) {
            result.append(object);
        }
        return result.toString();
    }

    /**
     * 去掉字符串里面所有的空格，包括字符串中间的空格
     */
    public static String trimAllBlank(String str) {
        if (str == null) {
            return null;
        }
        return str.replaceAll("\\s", "");
    }

    public static String[] split(String toSplit, String delimiter) {
        if (isEmpty(toSplit) || isEmpty(delimiter)) {
            return null;
        }
        int offset = toSplit.indexOf(delimiter);
        if (offset < 0) {
            return null;
        }

        String beforeDelimiter = toSplit.substring(0, offset);
        String afterDelimiter = toSplit.substring(offset + delimiter.length());
        return new String[]{beforeDelimiter, afterDelimiter};
    }

    private static String changeFirstCharacterCase(String str, boolean capitalize) {
        if (isEmpty(str)) {
            return str;
        }

        char baseChar = str.charAt(0);
        char updatedChar;
        if (capitalize) {
            updatedChar = Character.toUpperCase(baseChar);
        } else {
            updatedChar = Character.toLowerCase(baseChar);
        }
        if (baseChar == updatedChar) {
            return str;
        }

        char[] chars = str.toCharArray();
        chars[0] = updatedChar;
        return new String(chars);
    }

    public static String capitalize(String name) {
        return changeFirstCharacterCase(name, true);
    }

    public static String getStringFirstLower(String str) {
        if (StrUtil.isNotBlank(str)) {
            return str.substring(0, 1) + str.substring(1, 2).toLowerCase() + str.substring(2);
        }
        return str;
    }

}
